package com.feinno.fpa;

import com.feinno.database.Database;
import com.feinno.util.CollectionUtils;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Lei Gao on 8/12/15.
 */
public class FpaResult {
	String module;
	String subModule;
	FpaType type;
	String function;
	String function0;
	String returnType;
	List<String> DETs = new ArrayList<>();
	List<String> DETs0 = new ArrayList<>();
	List<String> FTRs = new ArrayList<>();
	List<String> FTRs0 = new ArrayList<>();

	public FpaResult clone() {
		FpaResult r = new FpaResult();
		r.module = this.module;
		r.subModule = this.subModule;
		r.function = this.function;
		r.function0 = this.function0;
		r.DETs = CollectionUtils.cloneList(this.DETs);
		r.DETs0 = CollectionUtils.cloneList(this.DETs0);
		r.FTRs = CollectionUtils.cloneList(this.FTRs);
		r.FTRs = CollectionUtils.cloneList(this.FTRs0);
		return r;
	}

	public boolean attachIlf(ILF ilf, FpaDictionary dict) {
		if (ilf == null) {
			return false;
		}
		for (String field : ilf.fields) {
			DETs0.add(field);
			DETs.add(dict.translate(field));
		}
		FTRs0.add(ilf.packageName + "." + ilf.className);
		FTRs.add(dict.translate(ilf.className));
		return true;
	}


	public void insert(Database db, String version) {
		String sql = "insert into FPA_Results (version, module, subModule, function, function0, type, DETs, DETs0, FTRs, FTRs0)" +
				" values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
		try {
			db.executeNonQuery(sql, version, module, subModule, function, function0, type.name(), compose(DETs), compose(DETs0), compose(FTRs), compose(FTRs0));
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	private String compose(List<String> ss) {
		String r = "";
		for (int i = 0; i < ss.size(); i++) {
			r = r + ss.get(i);
			if (i != ss.size() - 1) {
				r = r + "、";
			}
			if (r.length() > 400) {
				break;
			}
		}
		return r;
	}

	public FpaType analyzeType() {
		return FpaType.EI;
	}
}

